$(function(){
	dictionaryStart("model","4002");
	dictionaryStart("productionfactory","4003");
	dictionaryStart("towerheight","4001");
	picStart("4004");
	dgtStart(companyId);
	$('#predevicetype').bind('change',function(){
		predeviceStart(companyId);
	});
	/*$('#predevicecode').bind('change',function(){
		fallow();
	});*/
	$('#physicaltowercode').bind('change',function(){
		issametower();
	});
	/*$('#devicename').bind('blur',function(){
		appendtogt();
	});*/
	$('#picfile').bind('change',function(){
		getPicPath();
	});
	/*$('#devicename').bind('blur',function(){
		getDeviceName($('#devicename').val());
	});*/
	$("#model").bind("blur",function(){
		isamorphous();
	});
});

var dgtStart=function(companyId){
	$.ajax({
		url : contextPath+"user/dyxl/dlist",
		data : {
			companyId : companyId,
			devicetype : byqtype,
			transformercode : transformercode
		},
		type : 'post',
		success : function(data){
			if(data!=null&&data.length>0){
				$("#physicaltowercode").empty().show();
				if(!physicaltowercode){
					var ot ='<option value="-99">当前杆</option>';
					$('#physicaltowercode').append(ot);
				}
				if(typeof(isPhysicaltower)!='undefined'&&isPhysicaltower){
					var ot ='<option value="-99">当前杆</option>';
					$('#physicaltowercode').append(ot);
				}
				//$('#predevicecode').empty().show();
				for(var i=0;i<data.length;i++){
					if(data[i].deviceid==deviceid){
						$('#indexnumber').val(data.length+1);
					}
					$.ajax({
						url : contextPath+"user/dygt/getphysicaltowerlist",
						data : {
							deviceid : data[i].deviceid
						},
						type : 'POST',
						success:function(list){
							//alert(list.length);
							if(null!=list && list.length>0){
								var option="";
								for(var i=0;i<list.length;i++){
									option += '<option value="'+list[i].deviceid+'">'+list[i].note+'</option>';
								}
								$('#physicaltowercode').append(option);
								//alert(physicaltowercode);
								if(physicaltowercode){
									$('#physicaltowercode').val(physicaltowercode);
								}
								//$('#predevicecode').append(option);
							}
						}
					});
				}/*
				setTimeout(function(){
					fallow();
				},100);*/
			}
		}
	});
	
};

var predeviceStart=function(companyId){
	var sltype= $("#predevicetype").val();
	if("16"==sltype){
		sltype="dypdx";
	}else if("17"==sltype){
		sltype="dydlfjx";
	}else if("1"==sltype){
		sltype="pdbyq";
	}else if("2"==sltype){
		sltype="zsbyq";
	}else{
		sltype="dygt";
	}
	if("pdbyq"==sltype||"zsbyq"==sltype){
		$('#predevicecode').empty().show();
		var option="<option value='"+transformercode+"'>"+transformername+"</option>";
		$('#predevicecode').append(option);
		if(predevicecode)
			$('#predevicecode').val(predevicecode);
	}else{
		$('#predevicecode').empty().show();
		$.ajax({
			url : contextPath+"user/"+sltype+"/dlist",
			data : {
				companyId : companyId,
				deviceid : deviceid,
				currentDeviceId : currentDeviceId
			},
			type : 'POST',
			success:function(list){
				//alert(list.length);
				if(null!=list && list.length>0){
					var option="";
					for(var i=0;i<list.length;i++){
						if(typeof thisDeviceid!='undefined'){
							if(thisDeviceid!=list[i].deviceid){
								option += '<option value="'+list[i].deviceid+'">'+list[i].devicename+'</option>';
							}
						}else{
							option += '<option value="'+list[i].deviceid+'">'+list[i].devicename+'</option>';
						}
					}
					$('#predevicecode').append(option);
					if(predevicecode)
						$('#predevicecode').val(predevicecode);
				}
			}
		});
	}
};


function getPicPath(){
	$('#kzsx_add_pic_text').val($('#picfile').val());
}

function uploadPic(){
	var imgcount=$(".img_p").length+1;
	var pic='picfile';
	var picfile=$('#'+pic).val();
	var deviceName=$('#devicename').val();
	if(picfile==''){
		alert('请添加设备图片');
		return;
	}
	var photoType=picfile.substring(picfile.lastIndexOf("."),picfile.length).toUpperCase();
	//alert(photoType);
	if(photoType!=".JPG"&&photoType!=".GIF"){
		alert("添加的图片格式错误");
		return;
	}
	if(deviceName==''){
		alert("请输入设备名称");
		return;
	}
	if(imgcount>6){
		alert("添加图片已达极限");
		return;
	}
	changePicWidth();
	var pictype=$('#pictype').val();
	$.ajaxFileUpload({
		/*url : contextPath+"user/picture/upload?devicename="+deviceName+"&pictype="+pictype+"&imgcount="+imgcount,*/
		url : contextPath+"user/picture/upload",
		secureuri : false,
		fileElementId : pic,
		data : {data : [$('#'+pic).attr('name')],
				devicename : deviceName,
				pictype : pictype,
				imgcount : imgcount
		},
		dataType : 'json',
		contentType: "application/x-www-form-urlencoded; charset=utf-8",
		success : function(data,status){
			//alert(data);
			if(data!=null){
				//alert(data.msgDesc);
				//$('#inner_box').css('width',120*imgcount+'px');
				var option="";
				option += '<div class="img_p"><img class="small_pic" id="number'+imgcount+'" src="'+basePath+'upload/'+data.resourcepicname+'" alt="" />';
				option +='<img class="img_del" onclick="imgdel(this)" src="'+basePath+'user/images/add_members_close.png" />';
				//option += '<div class="img_p"><div class="top_div"><img class="small_pic" onload="autoSize(this,98)" alt="" />';
				option += '<p>'+data.msg+'</p></div>';
				$('#clear').before(option);
				if(imgcount>3){
					var kzsx_img_left=-120*(imgcount-3);
					$('.kzsx_cont_leftbox .img_flash_box .center .inner_box').stop().animate({'left':kzsx_img_left+'px'},200);
				}
				//alert(kzsx_img_cnt);
			}
		}
	});
} 


function isamorphous(){
	var amorphous=$("#model").val()+"";
	if(amorphous!=""&&amorphous!=null&&amorphous.indexOf('H')>=0){
		$("#isamorphous").val("是");
		$("#isamorphous1").val("是");
	}else{
		$("#isamorphous").val("否");
		$("#isamorphous1").val("否");
	}
}

function add(){
	var devicename=$("#devicename").val();
	var devicenumber=$("#devicenumber").val();
	var localcity=$("#localcity").val();
	var operationunit=$("#operationunit").val();
	var maintenanceteam=$("#maintenanceteam").val();
	var voltagelevel=$("#voltagelevel").val();
	var commissioningdate=$("#commissioningdate").val();
	var span=$("#span").val();
	var groundingresistance=$("#groundingresistance").val();
	var assetnumber=$("#assetnumber").val();
	if(devicename==""){
		alert('设备名称为空');
		return;
	}
	if(!validateLon($(":input[name='longitude']").val())){
		alert("经度范围为-180到180");
		return;
	}
	if(!validateLat($(":input[name='latitude']").val())){
		alert("纬度范围为-90到90");
		return;
	}
//	if(devicenumber==""){
//		alert('运行编号为空');
//		return;
//	}
	/*if(localcity==""){
		alert('所属地市为空');
		return;
	}
	if(operationunit==""){
		alert('运维单位为空');
		return;
	}
	if(maintenanceteam==""){
		alert('维护班组为空');
		return;
	}*/
	/*if(!isVoltage(voltagelevel)){
		alert("电压等级不正确，如220v或1kv");
		return;
	}*/
	/*if(commissioningdate==""){
		alert('投运日期为空');
		return;
	}*/
	/*if(!isNum(span)){
		alert('档距只能为数字');
		return;
	}
	if(!isNum(groundingresistance)){
		alert('接地电阻只能为数字');
		return;
	}
	if(assetnumber==""){
		alert('资产编号为空');
		return;
	}*/
	
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/dygt/add",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				if($('#inner_box .img_p').length>0){
					var count=1;
					$('#inner_box .img_p p').each(function(){
						var picname=$(this).text();
						var resourcepicname=$("#number"+count).attr("src").substring($("#number"+count).attr("src").lastIndexOf("/")+1);
						var linecode=$('#linecode').val();
						var devicecode=data.deviceid;
						var devicename=$('#devicename').val();
						//alert(picname);
						$.ajax({
							url : contextPath+'user/picture/add',
							type : 'post',
							async: false,
							data : {
								picname : picname,
								resourcepicname : resourcepicname,
								linecode : linecode,
								deviceid : devicecode,
								devicename : devicename,
								devicetype : 4
							},
							success : function(data){
								//alert(data.msgDesc);
								count++;
							}
						});
					});
				}
				location.href=contextPath+"user/d/dygtlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
			}
		}
	});
}

function edite(){
	var did=$("input[name='deviceid']").val();
	setStatus(did);
	var devicename=$("#devicename").val();
	var devicenumber=$("#devicenumber").val();
	var localcity=$("#localcity").val();
	var operationunit=$("#operationunit").val();
	var maintenanceteam=$("#maintenanceteam").val();
	var voltagelevel=$("#voltagelevel").val();
	var commissioningdate=$("#commissioningdate").val();
	var span=$("#span").val();
	var groundingresistance=$("#groundingresistance").val();
	var assetnumber=$("#assetnumber").val();
	if(devicename==""){
		alert('设备名称为空');
		return;
	}
	if(!validateLon($(":input[name='longitude']").val())){
		alert("经度范围为-180到180");
		return;
	}
	if(!validateLat($(":input[name='latitude']").val())){
		alert("纬度范围为-90到90");
		return;
	}
//	if(devicenumber==""){
//		alert('运行编号为空');
//		return;
//	}
	/*if(localcity==""){
		alert('所属地市为空');
		return;
	}
	if(operationunit==""){
		alert('运维单位为空');
		return;
	}
	if(maintenanceteam==""){
		alert('维护班组为空');
		return;
	}
	if(!isVoltage(voltagelevel)){
		alert("电压等级不正确，如220v或1kv");
		return;
	}
	if(commissioningdate==""){
		alert('投运日期为空');
		return;
	}
	if(!isNum(span)){
		alert('档距只能为数字');
		return;
	}
	if(!isNum(groundingresistance)){
		alert('接地电阻只能为数字');
		return;
	}
	if(assetnumber==""){
		alert('资产编号为空');
		return;
	}*/
	
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/dygt/edite",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				if($('#inner_box .img_p').length>0){
					$('#inner_box .img_p p').each(function(){
						var picname=$(this).text();
						var linecode=$('#linecode').val();
						var devicecode=data.deviceid;
						var resourcepicname=$(this).parent().children(".small_pic").attr("src").substring($(this).parent().children(".small_pic").attr("src").lastIndexOf("/")+1);
						var devicename=$('#devicename').val();
						//alert(picname);
						var pid=$(this).parent().children(".pid").val();
//						alert(pid);
						if(typeof(pid)=='undefined'){
							pid="";
						}
						$.ajax({
							url : contextPath+'user/picture/edite',
							type : 'post',
							data : {
								picname : picname,
								resourcepicname : resourcepicname,
								linecode : linecode,
								deviceid : devicecode,
								devicename : devicename,
								pid :pid,
								devicetype : 4
							},
							success : function(data){
								//alert(data.msgDesc);
							}
						});
					});
					$.ajax({
						url : contextPath+'user/picture/delete',
						type : 'post',
						async:false,
						data :{
							deviceid : data.deviceid
						},
						success : function(){
							location.href=contextPath+"user/d/dygtlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
						}
					});
				}else{
					$.ajax({
						url : contextPath+'user/picture/delete',
						type : 'post',
						async:false,
						data :{
							deviceid : data.deviceid
						},
						success : function(){
							location.href=contextPath+"user/d/dygtlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
						}
					});
				}
			}
		}
	});
}

function setDefault(elementId,data){
	$('#'+elementId).val(data);
}

/*function appendtogt(){
	var devicename=$('#devicename').val();
	var deviceId=$('#deviceid').val();
	if(devicename==''){
		return;
	}
	if(typeof(deviceId)==''){
		return;
	}
	var option='';
	option +='<option value="'+deviceId+'">'+devicename+'</option>';
	$('#physicaltowercode').append(option);
	issametower();
}*/
function issametower(){
	var towerid=$("#physicaltowercode").val();
	if(towerid=='same'){
		$('#issametower').val('是');
		$('#sametowernumber').val(0);
	}else{
		$('#issametower').val('否');
		$.ajax({
			url : contextPath+'user/dygt/getsametowernumber',
			type : 'post',
			data : {
				deviceid : towerid
			},
			success : function(data){
				$('#sametowernumber').val(data);
			}
		});
	}
}